package ctrip;

import java.util.Scanner;

public class MinLength2 {

	public static void main(String[] args) {
		Scanner scanner = new Scanner(System.in);
		int nodeCount;
		int[][] linjie;
		String allNumber;
		String[] numbers;
		while (scanner.hasNext()) {
			nodeCount = scanner.nextInt();
			scanner.nextLine();
			linjie = new int[nodeCount][nodeCount];
			for (int i = 0; i < linjie.length; i++) {
				allNumber = scanner.nextLine();
				numbers = allNumber.split(",");
				for (int j = 0; j < linjie.length; j++) {
					linjie[i][j] = Integer.parseInt(numbers[i]);
				}
			}
			for (int i = 0; i < linjie.length; i++) {
					linjie[i][i] = Integer.MAX_VALUE;
			}
			prim(linjie, nodeCount);
		}
		scanner.close();
	}

	public static void prim(int[][] matrix, int n) {
		int[] lowCost = new int[n];
		int[] closeSet = new int[n];

		int i, j, k, min, sum = 0;

		for (i = 1; i < n; i++) {
			lowCost[i] = matrix[0][i];
			closeSet[i] = 1;
		}

		for (i = 1; i < n; i++) {
			min = Integer.MAX_VALUE;
			k = 0;
			for (j = 1; j < n; j++) {
				if (lowCost[j] < min && lowCost[j] != 0) {
					min = lowCost[j];
					k = j;
					sum += min;
				}
			}

			lowCost[k] = 0;
			closeSet[k] = 0;
			for (j = 1; j < n; j++) {
				if (matrix[k][j] < lowCost[j] && matrix[k][j] != 0) {
					lowCost[j] = matrix[k][j];
					closeSet[j] = k;
				}
			}
		}
		System.out.println(sum);
	}
}
